<?php
namespace app\admin\controller;

/**
 * Class Config
 * @package app\admin\controller
 * @property \app\admin\service\Config $service
 */
class Config extends Admin
{

	protected $noNeedLogin = [];
	protected $noNeedAuth = ['*'];

	public function index()
	{
		if ( $this->request->isAjax() ){
		if($this->request->has('keyword','post')){
				$keyword = $this->request->post('keyword');
				$field = $this->request->post('field');
				if(!$field){
					$where = [
						'key'=>$keyword,
						'title' => $keyword
					];
				}else{
					$where[$field] = $keyword;
				}
			}else{
				$tag = $this->request->get('group/s','');
				if($tag){
					$where['group'] = $tag;
				}else{
					$where = [];
				}
			}
			return $this->service->getTableList($where);
		}

		$this->assign('group',config('app.config_group'));
		return $this->fetch();
	}
	
	public function create()
	{
		if ( $this->request->isPost() ){
			return $this->service->create();
		}

		$this->assign('html',\think\facade\Config::get('html'));
		$this->assign('group',\think\facade\Config::get('app.config_group'));
		return $this->fetch();
	}
	
	public function update($id=null)
	{
		if ( $this->request->isPost() ){
			$this->service->update();
		}
		$info = $this->service->getModel()->find($id);
		$this->assign('info', $info);
		$this->assign('html',\think\facade\Config::get('html'));
		$this->assign('group',\think\facade\Config::get('app.config_group'));
		return $this->fetch();
	}
	
	public function delete(){
	    //TODO:删除总是失败
		return $this->service->delete();
	}
	
}
